<template>
  <el-dialog
    title="新增员工"
    :visible="visible"
    width="40%"
    @close="onClose"
  >
    <!-- 表单 -->
    <el-form ref="form" label-width="120px" :rules="rules" :model="form">
      <el-form-item label="姓名" prop="username">
        <el-input v-model="form.username" style="width:80%" placeholder="请输入姓名" />
      </el-form-item>
      <el-form-item label="手机" prop="mobile">
        <el-input v-model="form.mobile" style="width:80%" placeholder="请输入手机号" />
      </el-form-item>
      <el-form-item label="入职时间" prop="timeOfEntry">
        <el-date-picker v-model="form.timeOfEntry" style="width:80%" placeholder="请选择入职时间" />
      </el-form-item>
      <el-form-item label="聘用形式" prop="formOfEmployment">
        <el-select v-model="form.formOfEmployment" style="width:80%" placeholder="请选择">
          <el-option v-for="item in hireType" :key="item.id" :label="item.value" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="工号" prop="workNumber">
        <el-input v-model="form.workNumber" style="width:80%" placeholder="请输入工号" />
      </el-form-item>
      <el-form-item label="部门" prop="departmentName">
        <el-input v-model="form.departmentName" style="width:80%" placeholder="请选择部门" @focus="onFocus" />
        <el-tree v-if="isShowTree" :data="treeData" :props="{label:'name'}" @node-click="onTreeNode" />
      </el-form-item>
      <el-form-item label="转正时间" prop="correctionTime">
        <el-date-picker v-model="form.correctionTime" style="width:80%" placeholder="请选择转正时间" />
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="$emit('update:visible',false)">取 消</el-button>
      <el-button type="primary" @click="onSave">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { getDepartmentsList } from '@/api/department'
import { transListToTree } from '@/utils'
import { addEmployees } from '@/api/employees'
import employees from '@/api/constant/employees'

export default {
  props: {
    visible: {
      type: Boolean,
      required: true
    }
  },
  data() {
    return {
      form: {
        correctionTime: '',
        timeOfEntry: '',
        departmentName: '',
        workNumber: '',
        formOfEmployment: '',
        mobile: '',
        username: ''
      },
      rules: {
        username: [
          { required: true, message: '用户姓名不能为空', trigger: 'blur' },
          { min: 1, max: 4, message: '用户姓名为1-4位' }],
        mobile: [
          { required: true, message: '手机号不能为空', trigger: 'blur' },
          { pattern: /^1[3-9]\d{9}$/, message: '手机号格式不正确', trigger: 'blur' }],
        formOfEmployment: [
          { required: true, message: '聘用形式不能为空', trigger: 'change' }],
        workNumber: [
          { required: true, message: '工号不能为空', trigger: 'blur' }],
        departmentName: [
          { required: true, message: '部门不能为空', trigger: ['change,blur'] }],
        timeOfEntry: [
          { required: true, message: '入职时间', trigger: 'blur' }],
        correctionTime: [
          { required: true, message: '转正时间', trigger: 'blur' }]
      },
      // 树形数据
      treeData: [],
      // 是否展示树
      isShowTree: false,
      loading: false,
      // 聘用形式
      hireType: employees.hireType
    }
  },

  created() {

  },

  methods: {
    // 关闭弹层
    onClose() {
      this.$emit('update:visible', false)
      // 重置表单
      this.$refs.form.resetFields()
      // 关闭弹层
      this.isShowTree = false
    },
    async  onFocus() {
      this.loading = true
      this.isShowTree = true
      const res = await getDepartmentsList()
      this.treeData = transListToTree(res.depts, '')
      this.loading = false
    },
    // 树形节点被点击
    onTreeNode(data) {
      this.form.departmentName = data.name
      this.isShowTree = false
    },
    // 点击保存
    async  onSave() {
      try {
        await this.$refs.form.validate()
        await addEmployees(this.form)
        this.$emit('update:visible', false)
        this.$emit('add-success')
        // this.$parent.loadEmployeesList()
        // this.$parent.addEmployeesDialogVisible=false
        // 指定父组件唯一，只有一个时，可以直接调用父组件的方法
      } catch (err) {
        console.log(err)
      }
    }
  }
}
</script>

<style scoped lang='less'>

</style>
